home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10998 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  39 lines

  1. Newsgroups: comp.lang.c++,comp.os.linux.development.apps,comp.os.linux.misc
  2. Path: indra.com!!bear
  3. From: bear@ (Bear Giles)
  4. Subject: Re: Are str* functions okay in C++?
  5. Message-ID: <Do54G9.4Ly.0.server@indra.com>
  6. Sender: usenet@indra.com (System Operator)
  7. Organization: I need to put my ORGANIZATION here.
  8. Date: Tue, 12 Mar 1996 05:55:20 GMT
  9. References: <4hpvi0$gt6@news.platinum.com>
  10. X-Nntp-Posting-Host: net.indra.com
  11.  
  12. In article <4hpvi0$gt6@news.platinum.com>,
  13. Mark Juric <dcmark@platinum.com> wrote:
  14. >    path = new char[strlen(mbuf)+1];
  15. >    strcpy(path,mbuf);
  16.  
  17. One quick note: there's a "strdup()" function which handles this
  18. for you, although it uses "free" instead of "delete."
  19.  
  20. Also, another useful technique for this type of code is:
  21.  
  22. void procedure (argument...)
  23.    {
  24.    static char * buffer = 0;
  25.  
  26.    if (buffer == 0)
  27.        buffer = malloc (2048);  /* or any _large_ number */
  28.  
  29.    ...
  30.    sprintf (buffer, format,...)
  31.    path = strdup (buffer);
  32.    ...
  33.  
  34. The idea is that "buffer" is a captive memory leak -- you allocate a
  35. large buffer once and never release it.  That increases the runtime
  36. size of your program, but eliminates the performance hit and possible
  37. memory leaks from repeatedly allocating and deleting small blocks.
  38.  
  39.